package drukarnia.controller;

import java.sql.Date;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.List;


import org.hibernate.Query;
import org.hibernate.Session;

import drukarnia.model.Orders;
import drukarnia.model.PriceForMaterials;
import drukarnia.model.SizeInM2Tmp;
import drukarnia.model.User;
import drukarnia.util.HibernateUtil;

public class OrderManager extends HibernateUtil{
	
	public Orders saveOrder(Orders order,  int id_user, String material, String fileName ){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		List<PriceForMaterials> pricesList = null;
		Query q = session.createQuery("from PriceForMaterials where typeOfMaterial='" + material + "'");
		pricesList = q.list();
		int id_price = 0;
		double materialCost = 0.0;
		for (PriceForMaterials priceForMaterials : pricesList) {
			id_price = priceForMaterials.getId_price();
			materialCost = priceForMaterials.getPrice();
			System.out.println(materialCost);
		}
		
		List<SizeInM2Tmp> sizeList = null;
		Query q1 = session.createQuery("from SizeInM2Tmp where name='" + fileName + "'");
		sizeList = q1.list();
		
		double sizeInM2 = 0.0;
		if(!sizeList.isEmpty()){
			for (SizeInM2Tmp sizeInM2Tmp : sizeList) {
				sizeInM2 = sizeInM2Tmp.getValue();
			}
		}
		
		
		int copies = order.getCopies();
		
		if(copies <= 0){
			copies = 1;
		}
		DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
		symbols.setDecimalSeparator('.');
		DecimalFormat df = new DecimalFormat("#.##", symbols);
		
		
		double cost = (sizeInM2 * materialCost)*copies;  
		cost = Double.valueOf(df.format(cost));
		
		order.setCost(cost);
		System.out.println("Cost: " + cost);
		
		Date data = new Date(new java.util.Date().getTime());
		order.setDate(data);
		
		order.setStatus("Oczekuje na realizacje");
		
		User user = (User) session.load(User.class, id_user);
		order.setId_user(user);
		
		order.setName_order("Zlecenie -  " + user.getMail());
		PriceForMaterials typeOfMaterials = (PriceForMaterials) session.load(PriceForMaterials.class, id_price);
		order.setTypeOfMaterials(typeOfMaterials);
		session.save(order);
		
		session.getTransaction().commit();
		return order;
		
	}
	
	public List<Orders> fetchOrdersFromDB(int id_user){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Orders> ordersList = null;
		Query q = session.createQuery("from Orders where id_user = " + id_user + " ORDER BY(date) DESC");
		ordersList = q.list();
		session.getTransaction().commit();
		return ordersList;
		
	}

	public void deleteOrderFromDB(int id_order){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		
		Query q = session.createQuery("delete from Orders where id_order = " + id_order+"");
		q.executeUpdate();
		
		session.getTransaction().commit();
		
	}
	
	public List<Orders> fetchAllOrdersFromDB(){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Orders> ordersList = null;
		Query q = session.createQuery("from Orders ORDER BY(date) DESC");
		ordersList = q.list();
		session.getTransaction().commit();
		return ordersList;
		
	}
	
	public List<Orders> fetchOrdersDetailsFromDB(int id_order){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Orders> ordersList = null;
		Query q = session.createQuery("from Orders where id_order = " + id_order);
		ordersList = q.list();
		session.getTransaction().commit();
		return ordersList;
	}
	

	public Orders progressing(int id_order){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Orders> lista = null;
		
		Query q = session.createQuery("from Orders where id_order = " + id_order);
		lista = q.list();
		
		for (Orders or : lista) {
			
			Orders o = (Orders) session.load(Orders.class, or.getId_order());
			o.setStatus("w trakcie realizacji");
			session.update(o);
		}
		session.getTransaction().commit();
		return null;
		
	}
	
	public Orders finished(int id_order){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Orders> lista = null;
		
		Query q = session.createQuery("from Orders where id_order = " + id_order);
		lista = q.list();
		
		for (Orders or : lista) {
			
			Orders o = (Orders) session.load(Orders.class, or.getId_order());
			o.setStatus("zakonczono");
			session.update(o);
		}
		session.getTransaction().commit();
		return null;
		
	}
	

}
